from rest_framework.response import Response
from rest_framework.views import APIView

from course.models import Course
from course.ser import CourseSer
from home.models import Nav, Banner, Category
from home.ser import NavSer, BannerSer, CategorySer, CategorySerS


# Create your views here.
class TopView(APIView):
    def get(self, request):
        topnav = Nav.objects.filter(is_del=False, position=True).order_by('order')
        ser = NavSer(topnav, many=True)
        return Response(ser.data)


class FootView(APIView):
    def get(self, request):
        footnav = Nav.objects.filter(is_del=False, position=False).order_by('order')
        ser = NavSer(footnav, many=True)
        return Response(ser.data)


class BannerView(APIView):
    def get(self, request):
        banner = Banner.objects.filter(is_del=False).order_by('order')
        ser = BannerSer(banner, many=True)
        return Response(ser.data)


class CategoryView(APIView):
    def get(self, request):
        category = Category.objects.filter(is_del=False, parent=None).order_by('order')
        ser = CategorySer(category, many=True)
        for i in ser.data:
            i['children'] = i['children'][:3]
        return Response(ser.data)


class CateinfoView(APIView):
    def get(self, request):
        category = Category.objects.filter(is_del=False, id=request.GET.get('id')).first()
        ser = CategorySer(category)

        list1 = Category.objects.filter(is_del=False, parent=request.GET.get('id'))
        cate=[]
        for i in list1:
            cate.append(i.id)
        list2=Course.objects.filter(is_del=False, is_home=True,category__in=cate).order_by('order')
        ser2 = CourseSer(list2, many=True)
        mydata = ser.data
        mydata['cousrse'] = ser2.data[:4]
        return Response(mydata)
